import dayjs from 'dayjs'
// 扩展插件
import relativeTime from 'dayjs/plugin/relativeTime'
import duration from 'dayjs/plugin/duration'
import 'dayjs/locale/zh-cn'
import isSameOrBefore from 'dayjs/plugin/isSameOrBefore'
import isSameOrAfter from 'dayjs/plugin/isSameOrAfter'
import isBetween from 'dayjs/plugin/isBetween'
dayjs.extend(isSameOrBefore) // 相同或之前
dayjs.extend(isSameOrAfter) // 相同或之后
dayjs.extend(isBetween) // 是否在之间

function rangeNumber(from = 0, to = 0) {
  let range = []
  from = from >= 0 ? from : 1
  for (let i = from; i <= to; i++) {
    range.push(i)
  }
  return range
}

/** 根据显示的时间字段返回相关的时间列的可选选项. */
export function rangeTimeArray(dateStr, start, end, detail) {
  let startDayjs = dayjs(start)
  let endDayjs = dayjs(end)
  let dateDayjs = dayjs(dateStr)
  // 计算每一列表数组开始和结束的数字.
  let dateArray = {
    year: [],
    month: [],
    date: [],
    hour: [],
    minute: [],
    second: []
  }
  // 计算年
  // dateArray.push()
  // 计算月,这里要判断如果大于开始的时候,月就从开始计算,如果小于结束,就以结束计算,如果是在之间就从0开始.
  // 后面的月,天,时,分秒,都是同一道理.

  function getar(timeType) {
    let temp = []
    if (timeType == 'year') {
      temp = rangeNumber(startDayjs.get('year'), endDayjs.get('year'))
    } else if (timeType == 'month') {
      setd('month', 'year')
    } else if (timeType == 'date') {
      setd('date', 'month')
    } else if (timeType == 'hour') {
      setd('hour', 'date')
    } else if (timeType == 'minute') {
      setd('minute', 'hour')
    } else if (timeType == 'second') {
      setd('second', 'minute')
    }

    function setd(type, timeType) {
      let start = 0
      let end = 0
      let nowtm = dateDayjs
      if (dateDayjs.isSameOrBefore(startDayjs, timeType)) {
        nowtm = startDayjs
        start = startDayjs.get(type)
        end = startDayjs.endOf(timeType).get(type)
        if (nowtm.isSame(endDayjs, timeType)) {
          end = endDayjs.get(type)
        }
      } else if (dateDayjs.isSameOrAfter(endDayjs, timeType)) {
        nowtm = endDayjs
        start = endDayjs.startOf(timeType).get(type)
        end = endDayjs.get(type)
        if (nowtm.isSame(startDayjs, timeType)) {
          start = startDayjs.get(type)
        }
      } else {
        start = dateDayjs.startOf(timeType).get(type)
        end = dateDayjs.endOf(timeType).get(type)
      }
      temp = rangeNumber(start, end)
    }
    dateArray[timeType] = temp
  }
  let key = ''
  for (key in detail) {
    if (key == 'day') {
      key = 'date'
    }
    getar(key)
  }
  return dateArray
}

/**
 * 根据现有时间取得当前的索引位置顺序
 * @param tmArray
 * @param nowtime
 * @param detail
 */
export function getIndexNowbydate(tmArray, nowtime, detail) {
  const d = dayjs(nowtime)
  const intermediate = [
    ['year', detail.year],
    ['month', detail.month],
    ['date', detail.day],
    ['hour', detail.hour],
    ['minute', detail.minute],
    ['second', detail.second]
  ]

  const idx = intermediate.filter(m => m[1]).map(m => {
    const type = m[0]
    const index = tmArray[type].findIndex(n => n === d.get(type))
    return index === -1 ? 0 : index
  })

  return [
    ...idx,
    ...[0, 0, 0, 0, 0, 0]
  ].slice(0, 6)
}
/**
 * 根据现有索引值返回当前时间。
 * @param tmArray
 * @param nowtime
 * @param detail
 */
export function getNowbyIndex(tmArray, nowIndex, detail) {
  const intermediate = [
    ['year', detail.year],
    ['month', detail.month],
    ['date', detail.day],
    ['hour', detail.hour],
    ['minute', detail.minute],
    ['second', detail.second]
  ]
  function getValue(type) {
    const index = intermediate.filter(m => m[1]).findIndex(m => m[0] === type)
    if (index !== -1) {
      return tmArray[type][nowIndex[index]]
    }
    return tmArray[type][tmArray[type].length - 1]
  }

  let year = getValue('year')
  let month = getValue('month')
  let date = getValue('date')
  let hour = getValue('hour')
  let minute = getValue('minute')
  let second = getValue('second')

  let str = year +
		'/' +
		(month + 1) +
		'/' + date +
		' ' +
		hour +
		':' +
		minute +
		':' +
		second
  return dayjs(str).format('YYYY/MM/DD HH:mm:ss')
}
/**
 * 检查提供的时候是否是有效的时间段内，并返回正确的可用时间.
 * @param nowdate 当前时间
 * @param start 
 * @param end 
 */
export function checkNowDateisBetween(nowdate, start, end) {
  nowdate = dayjs(nowdate).isValid() ? dayjs(nowdate) : dayjs()
  start = dayjs(start)
  end = dayjs(end)
  if (nowdate.isSameOrBefore(start)) {
    return start.format('YYYY/MM/DD HH:mm:ss')
  }
  if (nowdate.isSameOrAfter(end)) {
    return end.format('YYYY/MM/DD HH:mm:ss')
  }
  return nowdate.format('YYYY/MM/DD HH:mm:ss')
}

